<template>
  <el-container class="home">

    <!-- 头部 -->
    <el-header class="header">
      <div class="header_logo">
        <img src="../assets/头像.jpg" alt="" srcset="">
        <span>bear</span>
      </div>
      <el-button type="info" @click="out">
        退出</el-button>
    </el-header>
    <el-container class="container">

      <!-- 左侧导航 -->
      <el-aside class="aside" :width="isfolding ? '70px' :'300px'">
        <!-- 折叠导航栏的按钮 -->
        <div class="folding" @click="folding">
          |||
        </div>
        <!-- 侧边导航菜单 -->
        <el-menu unique-opened router :collapse-transition='false' :collapse="isfolding"
          :default-active="highlighted" class="el-menu-vertical-demo" background-color="#313743"
          text-color="#fff" active-text-color="#409efc">
          <!-- 一级导航 -->
          <el-submenu :index="item.id +''" v-for="item in menuUl" :key="item.id">
            <!-- 图标及文本 -->
            <template slot="title">
              <i class="el-icon-menu"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级导航目录 -->
            <el-menu-item :index='`/${items.path}`' v-for="items in item.children" :key="items.id"
              @click="light(`/${items.path}`)">
              <i class="el-icon-menu"></i>
              <span>{{items.authName}}</span>
            </el-menu-item>
          </el-submenu>

        </el-menu>
      </el-aside>

      <!-- 右侧主体 -->
      <el-main class="main">
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'HoMe',
  data() {
    return {
      menuUl: [],
      menobj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-3702mima',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiaolock_fill',
      },
      isfolding: false,
    }
  },
  computed: {
    highlighted: () => {
      return window.sessionStorage.highlighted
    },
  },
  methods: {
    // 退出
    out() {
      window.sessionStorage.removeItem('token')
      this.$router.push('/login')
    },
    // 获取侧边栏数据
    async getMenu() {
      const { data: res } = await this.$http.get('menus')
      // console.log(res)
      if (res.meta.status != 200) {
        this.$message.error('出现错误，未获取数据信息，请重新登录')
      }
      this.menuUl = res.data
    },
    // 折叠侧边栏
    folding() {
      this.isfolding = !this.isfolding
    },
    // 传递高亮的默认路径
    light(e) {
      window.sessionStorage.highlighted = e
    },
  },
  created() {
    // 页面挂载前获取左侧列表数据
    this.getMenu()
  },
}
</script>

<style lang='less' scoped>
.home {
  height: 100vh;
}
.header {
  height: 80px !important;
  background: #363d40;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 50px;
  .header_logo {
    width: 220px;
    height: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #fff;
    img {
      width: 75px;
      height: 75px;
      border-radius: 100%;
    }
    span {
      font-size: 30px;
      font-weight: 600;
      letter-spacing: 3px;
    }
  }
}
.container {
  height: calc(100% - 80px);
}
.aside {
  height: 100%;
  background: #313743;
  .folding {
    cursor: pointer;
    background: #41485a;
    text-align: center;
    height: 40px;
    line-height: 40px;
    color: #fff;
    transition: all 0.6s;
    &:hover {
      color: #3e9efd;
    }
  }
}
.main {
  background: #e9edf1;
}
.el-menu {
  border-right: 0;
}
</style>